package com.ithema.manger3.dao;

import com.ithema.manger3.domain.Student;

public class StudentDao {
    //创建数组
    private static Student[] student = new Student[5];

    //添加学生信息
    public boolean addStudent(Student stu) {
        //定义初始值
        boolean flag = false;
        //遍历数组添加元素
        for (int i = 0; i < student.length; i++) {
            Student stus = student[i];
            if (stus == null) {
                student[i]= stu;//对象不能直接给对象赋值:例如stus=stu;错误
                flag = true;
                break;
               /*student[i]=stu;
               return true;*/
            }
        }return flag;
    }
//查看学生
    public Student[] findAllStudent() {
        return student;
    }

    //删除学生
    public void deleteStudent(String sid) {
        //调用getIndex方法判断学号对应的索引位置
        int index = getIndex(sid);
        //是数组对应索引值为null,即可删除
        student[index]=null;
    }
    //定义方法判断学号对应的索引位置
    public static int getIndex(String sid){
        //定义初始值
        int index=-1;
        //遍历数组
        for (int i = 0; i < student.length; i++) {
            if(student[i]!=null&&sid.equals(student[i].getSid())){
                index=i;
                break;
            }
        }return index;
    }
//修改学生信息
    public void updateStudent(String updateSid, Student updateStu) {
        //调用getIndex方法寻找新的学号对应的索引位置
        int index = getIndex(updateSid);
        //修改
        student[index]=updateStu;
    }
}
